<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>菜单管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/statics/layui/lib/layui-v2.5.5/css/layui.css"   media="all">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/statics/layui/css/layuimini.css?v=2.0.4.2" media="all">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/statics/layui/css/themes/default.css" media="all">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/statics/layui/lib/font-awesome-4.7.0/css/font-awesome.min.css" media="all">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/statics/layui/css/public.css" media="all">
    <%--    引入dtree样式--%>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/statics/layui_ext/dtree/dtree.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/statics/layui_ext/dtree/font/dtreefont.css">
</head>
<body>
    <div class="layuimini-container">
    <div class="layuimini-main">
        <div class="layui-row">
            <%-- 左侧菜单树 --%>
            <div class="layui-col-md2">
                <!-- 树节点容器开始 -->
                <ul id="menuTree" class="dtree" data-id="0" style="width: 240px;"></ul>
                <!-- 树节点容器结束 -->
            </div>

            <%-- 右侧数据表格 --%>
            <div class="layui-col-md10">



                <%-- 数据表格 --%>
                <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

                <%-- 行工具栏 --%>
                <script type="text/html" id="currentTableBar">
                    <a class="layui-btn layui-btn-xs data-count-edit" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
                    <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete"><i class="layui-icon layui-icon-close"></i>删除</a>
                </script>

                <%-- 添加和修改窗口 --%>
                <div style="display: none;padding: 5px" id="addOrUpdateWindow">
                    <form class="layui-form" style="width:90%;" id="dataFrm" lay-filter="dataFrm">
                        <%-- 菜单编号 --%>
                        <input type="hidden" name="id">
                        <div class="layui-form-item">
                            <label class="layui-form-label">父级菜单</label>
                            <div class="layui-input-block">
                                <%--  代替存储下拉菜单值--%>
                                <input type="hidden" name="pid" id="pid">
                                <ul id="menuSelectTree" class="dtree" data-id="0"></ul>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">菜单名称</label>
                            <div class="layui-input-block">
                                <input type="text" name="title" lay-verify="required" autocomplete="off" placeholder="请输入菜单名称" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">菜单地址</label>
                            <div class="layui-input-block">
                                <input type="text" name="href"  autocomplete="off" placeholder="请输入菜单地址" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">菜单图标</label>
                            <div class="layui-input-block">
                                <%--代替存储菜单图标值--%>
                                <input type="hidden" name="icon" id="icon">
                                <input type="text" name="icon" id="iconPicker" lay-filter="iconPicker" autocomplete="off" placeholder="请输入菜单图标" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">是否展开</label>
                                <div class="layui-input-block">
                                    <input type="radio" name="spread" value="1" title="是">
                                    <input type="radio" name="spread" value="0" title="否" checked>
                                </div>
                            </div>
                        </div>

                        <div class="layui-form-item layui-row layui-col-xs12">
                            <div class="layui-input-block" style="text-align: center;">
                                <button type="button" class="layui-btn" lay-submit lay-filter="doSubmit"><span class="layui-icon layui-icon-add-1"></span>提交</button>
                                <button type="reset" class="layui-btn layui-btn-warm"><span class="layui-icon layui-icon-refresh-1"></span>清空</button>
                                <button type="button" class="layui-btn layui-btn-danger" id="resetMenu"><span class="layui-icon layui-icon-return"></span>重置菜单</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

</body>

<%-- 导入layui的js文件--%>
<script src="${pageContext.request.contextPath}/statics/layui/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
    <script>
        layui.extend({
            dtree:"${pageContext.request.contextPath}/statics/layui_ext/dtree/dtree",
            iconPickerFa :'${pageContext.request.contextPath}/statics/layui/js/lay-module/iconPicker/iconPickerFa'
        }).use(['form','jquery','table','layer','dtree','iconPickerFa'],function() {
            var form=layui.form,
                $=layui.jquery,
                table=layui.table,
                layer=layui.layer,
                dtree=layui.dtree;
                iconPickerFa=layui.iconPickerFa;

            //渲染左侧菜单树
            var menuTree=dtree.render({
                elem: "#menuTree",//绑定ul标签的ID属性值
                url: "/admin/menu/loadMenuTree", //请求地址
                dataStyle: "layuiStyle", //使用layui风格的数据格式
                dataFormat: "list", //配置data的风格为list
                response:{message:"msg",statusCode:0}, //修改response中返回数据的定义
            });

            //渲染表格组件
            var tableIns = table.render({
                elem: '#currentTableId',
                url: '${pageContext.request.contextPath}/admin/menu/list',
                toolbar: '#toolbarDemo',
                cols: [[
                    {field: 'id', width: 100, title: '菜单编号', align: "center"},
                    {field: 'title', width: 180, title: '菜单名称', align: "center"},
                    {field: 'href', minWidth: 200, title: '菜单地址', align: "center"},
                    {field: 'spread', width: 100, title: '是否展开', align: "center",templet:function(d) {
                        return d.spread==1?"<span style='color: #0040ff'>是</span>":"<span style='color: red'>否</span>"
                    }},
                    {field: 'icon', width: 100, title: '菜单图标', align: "center",templet:function(d) {
                        return "<i class='"+d.icon+"'></i>";
                    }},
                    {title: '操作', minWidth: 200, toolbar: '#currentTableBar', align: "center"}
                ]],
                page: true,
                done: function (res, curr, count) {
                    //判断当前页码是否是大于1并且当前页的数据量为0
                    if (curr > 1 && res.data.length == 0) {
                        var pageValue = curr - 1;
                        //刷新数据表格的数据
                        tableIns.reload({
                            page: {curr: pageValue}
                        });
                    }
                }
            });
            //渲染父级下拉菜单
            var menuSelectTree = dtree.renderSelect({
                elem: "#menuSelectTree",//绑定ul标签的ID属性值
                url: "/admin/menu/loadMenuTree", //请求地址
                dataStyle: "layuiStyle", //使用layui风格的数据格式
                dataFormat: "list", //配置data的风格为list
                response:{message:"msg",statusCode:0}, //修改response中返回数据的定义
            });
            //监听下拉菜单点击事件
            dtree.on('node(menuSelectTree)',function(data) {
                $("#pid").val(data.param.nodeId)
            })

            //渲染图标选择器组件
            iconPickerFa.render({
                // 选择器，推荐使用input
                elem: '#iconPicker',
                url:"${pageContext.request.contextPath}/statics/layui/lib/font-awesome-4.7.0/less/variables.less",
                // 数据类型：fontClass/unicode，推荐使用fontClass
                type: 'fontClass',
                // 是否开启搜索：true/false
                search: true,
                // 点击回调
                click: function (data) {
                    $("#icon").val("fa "+data.icon)
                }
            });

            var url,//请求地址
                mainIndex;//打开窗口

            /**
            * 监听菜单树单击事件
            *   切换菜单列表范围
            */
            dtree.on("node('menuTree')" ,function(obj){
                tableIns.reload({
                    where:{"id":obj.param.nodeId},
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                })
                    console.log(obj.param.nodeId)
            });

            /**
            *监听编辑操作
            */
            table.on('tool(currentTableFilter)',function(obj){
                switch (obj.event) {
                    case "edit":
                        openUpdateWindow(obj.data);
                        break;
                    case "delete":
                        openDeleteWindow(obj.data);
                        break;
                }

            })

            /**
            * 打开修改窗口
            */
            function openUpdateWindow(data){
                mainIndex=layer.open({
                    type:1,
                    title:"修改菜单",
                    area:['800px',"500px"],
                    content:$("#addOrUpdateWindow"),
                    success:function() {
                        url="/admin/menu/updateMenu"
                        form.val("dataFrm",data),//数据回显
                        iconPickerFa.checkIcon('iconPicker', data.icon),//图标回显
                        dtree.dataInit("menuSelectTree",data.pid),
                        dtree.selectVal("menuSelectTree");//父级菜单回显
                        //判断当前选中节点是否是一级菜单，如果是一级菜单，那么该菜单是没有父级菜单
                        if(data.pid==0){
                            console.log(data)
                            menuSelectTree.reload();
                        }
                    }

                })
            };

            /**
             *打开删除窗口
             */
            function openDeleteWindow(data){
                mainIndex=layer.open({
                    title:"删除菜单",
                    content:"确定要删除【<span style='color: red'>"+data.title+"</span>】这个菜单吗？",
                    btn: ["<i class=\"layui-icon layui-icon-ok\"></i> 确定","<i class=\"layui-icon layui-icon-close\"></i> 取消"],
                    yes:function() {
                        url="/admin/menu/deleteMenu"
                        $.get(url,"menuId="+data.id,function(result){
                            if (result.success){//成功
                                tableIns.reload();
                                layer.close(mainIndex);
                            }
                            layer.msg(result.message);
                        },"json");
                    },
                    btn2:""
                })
            }

            //监听提交事件
            form.on("submit(doSubmit)",function(data) {
                console.log(data);
                data.field.icon="fa "+data.field.icon;
                $.post(url,data.field,function(result){
                    if (result.success){
                        tableIns.reload()//刷新数据
                        layer.close(mainIndex);//关闭页面
                    }
                    layer.msg(result.message);//提示信息
                },"json");
            })

    })
    </script>
</html>
